<template>
    <div>
        <h-back title='咨询港医'/>
        <v-progress-linear
            color="rgb(4, 190, 2)"
            v-show="progress"
            indeterminate
            rounded
            height="2"
        ></v-progress-linear>
        <v-container style="">
            <div
            max-width="90%"
            class="mx-auto co">
                <h2 class="text-center">{{ title }}</h2>
                <p style="padding:24px">{{ desc }}</p>
                <v-row>
                <v-col cols="12">

                </v-col>
                </v-row>
                <v-btn v-show="btn1.show" :loading="btn1.load" @click="btn1click" width="100%" color="rgb(4, 190, 2)" style="margin-top: 24px;color:#fff">{{ btn1.text }}</v-btn>
                <v-btn v-show="btn2.show" :loading="btn2.load" @click="btn2click" width="100%" color="rgb(4, 190, 2)" style="margin-top: 24px;color:#fff">{{ btn2.text }}</v-btn>
            </div>    
        </v-container>

    </div>
</template>
<script>
import { WorkOrderCreateOrder, WorkOrderQueryReceiveFreecount } from './../../api/api'
import { setTimeout } from 'timers';
import { truncate } from 'fs';
export default {
    data:()=>({
        progress:false,
        title:"载入中",
        desc:"",
        btn1:{
            show:false,
            text:'',
            load:false,
        },
        btn2:{
            show:false,
            text:'',
            load:false,
            href:'',
        },
        temp:{
            title:"非常抱歉",
            desc:'人工核保是一项付费服务！您可以通过个人中心加入利佳会员获得免费8次人工核保服务。或付费咨询（HKD$30.00/次。如您已是利佳会员，可能是您的免费咨询次数已用完。详情请咨询我们的在线客服。',
        }
    }),
    methods:{
        btn1click:function(){ // 创建订单
            this.btn1.load = true
            WorkOrderCreateOrder(this.$cookies.get("authtoken"))
            .then((resp)=>{
                this.btn1.load = false
                var res = resp.data
                console.log(res)
                if(res.code == 1){ // 没有错误
                    this.progress = false
                    this.title = res.data.title
                    this.desc = res.data.desc
                    if(res.data.total_fee == 0){ // 直接前往表单页面验证
                        this.$router.push('/workorder/from?order_no='+res.data.order_no) 
                        return
                    }
                    this.$router.push('/pay/wechat?order_no='+res.data.order_no+'&calluri=/workorder/from')
                    return
                }
                this.title = '发生了错误'
                this.desc = res.err
                return
            })
            .catch((err)=>{
                this.btn1.load = false
                this.title = '发生了错误'
                this.desc = err
            })
        },
        btn2click:function(){ // 加入会员
            this.$router.push(this.btn2.href)
        },
        back:function(){
            this.$router.go(-1)
        }
    },
    created:function(){
        // 查询或领取免费核保
        WorkOrderQueryReceiveFreecount(this.$cookies.get('authtoken'))
        .then((resp)=>{
            var res = resp.data
            this.progress = false
            this.$store.commit('setProgress',false)
            if(res.code === 1){
                if(res.data.free_count > 0){ // 走会员渠道
                    this.title = '会员免费咨询'
                    this.desc = '尊贵的会员，您剩余免费咨询次数还有' + res.data.free_count + '次'
                    this.btn1.text = '立即咨询'
                    this.btn1.show = true
                }else{ // 走支付渠道
                    this.btn1.text = '付费咨询'
                    this.btn1.show = true
                    this.btn2.text = '加入会员'
                    this.btn2.show = true
                    this.btn2.href = '/pay/member'
                    this.title = this.temp.title
                    this.desc = this.temp.desc
                }
                return
            }
            this.title = '系统错误'
            this.desc = res.err
            if(res.result.errcode == 2001){
                window.location.href = '/login'
            }
           return 
        })
        .catch((err)=>{
            this.progress = false
            this.title = '服务器错误'
            this.desc = err
        })
    }
}
</script>
<style>
.container{
    padding: 0 16px;
    margin-top: 100px;
}
.co{
    max-width: 90%;
    width: 90%;
    margin-top: 16px;
    margin-left: 5%;
    /*box-shadow: 0 5px 15px rgba(0,0,0,0.08);*/
}
</style>